<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: jinzhao
 * Date: 16-8-13
 * Time: 下午4:10
 */
class Home extends MY_Controller{
    public function index($curpage = 0, $perpage = 5)
    {
        $data = array(
            'templates' => array(),
            'links' => '',
            'address' => array(),
            'addr_links' => ''
        );

        $result = $this->template($curpage, $perpage, true);
        $result = json_decode($result,true);
        if ($result['code'] == 'ok'){
            $data['templates'] = $result['templates']; 
            $data['links'] = $result['links']; 
        }
        
        $result = $this->address($curpage, $perpage, true);
        $result = json_decode($result, true);
        if ($result['code'] == 'ok'){
            $data['address'] = $result['address'];
            $data['addr_links'] = $result['addr_links']; 
            
        }
        
        $data['email'] = $this->session->userdata('email');
        

        $this->load->view('index.html', $data);
    }
    
    
    public function template($curpage = 0, $perpage = 5, $return = false)
    {
        $data = array(
            'templates' => array(),
            'links' => ''
        );

        $this->load->model('template_model');
        
        $templates = $this->template_model->get($curpage, $perpage);
        if ($templates){
            $data['templates'] = $templates;
        }
        
        $links = $this->get_template_pagination($curpage, $perpage);
        if ($links){
            $data['links'] = $links;
        }

        $data['code'] = 'ok';
        $data = json_encode($data);
        if ($return){
            return $data;
        }
        echo $data;
    }
    
    private function get_template_pagination($curpage = 0, $perpage = 5)
    {
        $this->load->library('pagination');
        $conf['base_url'] = base_url('home/template');
        $numbers = $this->session->userdata('template_numbers');
        $conf['total_rows'] = $numbers === false ? 0 : $numbers;
        $conf['per_page'] = $perpage;
        $conf['cur_page'] = $curpage;
        $conf['first_tag_open'] = '<li class="get_page_template first-page">';
        $conf['last_tag_open'] = '<li class="get_page_template last-page">';
        $conf['next_tag_open'] = '<li class="get_page_template ">';
        $conf['prev_tag_open'] = '<li class="get_page_template ">';
        $conf['num_tag_open'] = '<li class="get_page_template ">';

        $this->pagination->initialize($conf);
        $links = $this->pagination->create_links();

        return $links;
    }
    
    public function address($curpage = 0, $perpage = 5, $return = false)
    {
        $data = array(
            'address' => array(),
            'addr_links' => ''
        );

        $this->load->model('address_model');

        $address = $this->address_model->get($curpage, $perpage);
        if ($address){
            $data['address'] = $address;
        }

        $links = $this->get_address_pagination($curpage, $perpage);
        if ($links){
            $data['addr_links'] = $links;
        }

        $data['code'] = 'ok';
        $data = json_encode($data);
        if ($return){
            return $data;
        }
        echo $data;
    }
    
    private function get_address_pagination($curpage = 0, $perpage = 5)
    {
        $this->load->library('pagination');
        $conf['base_url'] = base_url('home/address');
        $numbers = $this->session->userdata('address_numbers');
        $conf['total_rows'] = $numbers === false ? 0 : $numbers;
        $conf['per_page'] = $perpage;
        $conf['cur_page'] = $curpage;
        $conf['first_tag_open'] = '<li class="get_page_address first-page">';
        $conf['last_tag_open'] = '<li class="get_page_address last-page">';
        $conf['next_tag_open'] = '<li class="get_page_address">';
        $conf['prev_tag_open'] = '<li class="get_page_address">';
        $conf['num_tag_open'] = '<li class="get_page_address">';

        $this->pagination->initialize($conf);
        $links = $this->pagination->create_links();

        return $links;
        
    }
    
}